const AiModelService = require('./src/services/aiModelService');

async function testEmptyResponse() {
  console.log('测试空响应处理...');
  
  try {
    // 模拟空响应的情况
    const mockResponse = {
      data: {
        output: {
          choices: [
            {
              finish_reason: "stop",
              message: {
                role: "assistant",
                content: ""
              }
            }
          ]
        },
        usage: {
          total_tokens: 4608,
          output_tokens: 0,
          input_tokens: 4608
        },
        request_id: "70d153bc-37c2-9380-8d98-f03474e9e857"
      }
    };
    
    // 手动解析响应
    console.log('解析模拟的空响应...');
    
    if (mockResponse.data && 
        mockResponse.data.output && 
        mockResponse.data.output.choices && 
        mockResponse.data.output.choices.length > 0 && 
        mockResponse.data.output.choices[0].message && 
        mockResponse.data.output.choices[0].message.content !== undefined) {
      
      const content = mockResponse.data.output.choices[0].message.content;
      
      console.log('解析结果:');
      console.log('- 内容是否为空:', content === '');
      console.log('- 内容长度:', content.length);
      
      // 处理空内容的情况
      if (content === '') {
        console.log('检测到空响应，应该返回错误或使用备用方法');
      } else {
        console.log('响应内容:', content);
      }
    } else {
      console.log('响应格式不符合预期');
    }
    
    // 测试实际 API 调用
    console.log('\n测试实际 API 调用，使用较短的输入...');
    
    const response = await AiModelService.parseWithQwenLong(
      '这是一个简短的测试文本，用于测试通义千问 API 的响应。',
      { task: 'summarize', max_tokens: 100 }
    );
    
    console.log('API 响应结果:');
    console.log('- 成功状态:', response.success);
    console.log('- 结果长度:', response.result ? response.result.length : 0);
    console.log('- 结果内容:', response.result);
    
  } catch (error) {
    console.error('测试失败:', error.message);
  }
}

testEmptyResponse(); 